package Leetcode;

import java.util.*;

public class ThreeNum {
//三数之和等于0
    //转换为n个两数之和问题

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        //两个列表分别存放非负数和负数
        List<Integer> PosNums=new ArrayList<>(),NegNums=new ArrayList<>();
//        boolean HasZero = false;
        for (Integer i:nums){
            if (i>=0 && !PosNums.contains(i)) PosNums.add(i);
            if (i <0 && !NegNums.contains(i)) NegNums.add(i);
        }
//        System.out.println(PosNums);
//        System.out.println(NegNums);

        for (Integer i:PosNums){
//            System.out.println(i);
             for(int[] a:twoSum(NegNums,0-i)){
                 System.out.println(i);
                 System.out.println(a);
             }
        }
        for (Integer i:NegNums){
//            System.out.println(i);
            for(int[] a:twoSum(PosNums,0-i)){
                System.out.println(i);
                System.out.println(a);
            }
        }


        return ans;

    }

    public static void main(String[] args) {
        int[] nums = {-1,0,1,2,-1,-4};
        List<Integer> n = new ArrayList<>();
        for (Integer i:nums)n.add(i);
        ThreeNum t = new ThreeNum();
//        t.threeSum(nums);
        System.out.println(t.twoSum(n,0));

    }

    public List<int[]> twoSum(List<Integer> nums,int target){
        List<int[]> ans = new ArrayList<>();
        HashMap<Integer,Integer> map = new HashMap<>();
        HashSet<Integer> set = new HashSet<>();


        for (int i=0;i<nums.size();i++){
//            System.out.println(nums.get(i));

            if (set.contains(target-nums.get(i))) {
                System.out.println(nums.get(i));
                int [] p = new int[2];
                p[0] = nums.get(i);
                p[1] = target-nums.get(i);
                System.out.println(p);
                ans.add(new int[] {nums.get(i),target-nums.get(i)});

            }
            else set.add(nums.get(i));
        }

        return ans;
    }
}
